/**
 * 查询微服务列表
 */
function queryMicroserviceList() {
    $.ajax({
        type: 'GET',
        url: "/service/authorize/microservice/all",
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        success: function (data) {
            console.log(data);
            for (let i = 0; i < data.length; i++) {
                let microservice = data[i];

                let microservicePanelDiv = $(".microservice-component .templates .microservice-panel").clone();
                microservicePanelDiv.attr("microservice-id", microservice.id);
                microservicePanelDiv.css("display", "block");
                microservicePanelDiv.find(".panel-title").text(microservice.name);
                microservicePanelDiv.find(".id-value").text(microservice.id);
                microservicePanelDiv.find(".name-value").text(microservice.name);
                microservicePanelDiv.find(".context-path-value").text(microservice.contextPath);
                microservicePanelDiv.find(".description-value").text(microservice.description);

                microservicePanelDiv.find(".authority-list-show-button").click(function () {
                    if (microservicePanelDiv.find(".authority-list").hasClass("expansion")) {
                        microservicePanelDiv.find(".authority-list").removeClass("expansion");
                        $(this).removeClass("open");
                    }
                    else if (microservicePanelDiv.find(".authority-list").hasClass("queried")) {
                        microservicePanelDiv.find(".authority-list").addClass("expansion");
                        $(this).addClass("open");
                    }
                    else {
                        queryUris(microservicePanelDiv);
                    }
                });

                microservicePanelDiv.find(".refresh-service-button").click(function () {
                    refreshService(microservicePanelDiv);
                });

                microservicePanelDiv.find(".edit-button").click(function () {

                    let editMicroservicePanel = $(".microservice-component .templates .edit-microservice-panel").clone();
                    editMicroservicePanel.attr("microservice-id", microservice.id);
                    editMicroservicePanel.find(".panel-title").text("Modify Microservice");
                    editMicroservicePanel.find(".name-value").val(microservice.name);
                    editMicroservicePanel.find(".context-path-value").val(microservice.contextPath);
                    editMicroservicePanel.find(".description-value").val(microservice.description);

                    editMicroservicePanel.find(".save-microservice-button").click(function () {
                        updateMicroservice(editMicroservicePanel);
                    });

                    editMicroservicePanel.find(".cancel-microservice-button").click(function () {
                        editMicroservicePanel.remove();
                        microservicePanelDiv.css("display", "block");
                    });

                    editMicroservicePanel.insertBefore($(this).parents(".microservice-panel"));
                    microservicePanelDiv.css("display", "none");
                });

                microservicePanelDiv.find(".delete-button").click(function () {
                    PromptWindowUtils.showDeletePrompt("Are you sure you want to delete this microservice？", function () {
                        $.ajax({
                            type: "DELETE",
                            contentType: "application/json;charset=utf-8",
                            url: "/service/authorize/microservice/delete/" + microservice.id,
                            dataType: "json",
                            headers: {
                                Authorization: JwtTokenUtils.getBearerHeader()
                            },
                            success: function () {
                                $(".microservice-component .microservice-list").empty();
                                queryMicroserviceList();
                                PromptWindowUtils.hideDeletePrompt();
                            }
                        });
                    });
                });

                $(".microservice-component .microservice-list").append(microservicePanelDiv);
            }
        }
    });
}

/**
 * 查询服务uri列表
 * @param microservicePanel 微服务显示面板
 */
function queryUris(microservicePanel) {
    let microserviceId = microservicePanel.attr("microservice-id");
    $.ajax({
        type: "GET",
        url: "/service/authorize/authority/findByMicroservice/" + microserviceId,
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        success: function (data) {
            microservicePanel.find(".authority-list").empty();
            microservicePanel.find(".authority-list").addClass("expansion");
            if (!microservicePanel.find(".authority-list").hasClass("queried")) {
                microservicePanel.find(".authority-list").addClass("queried");
            }
            microservicePanel.find(".authority-list-show-button").addClass("open");
            for (let i = 0; i < data.length; i++) {
                let authority = data[i];
                let authorityPanel = $(".templates .authority-panel").clone();
                authorityPanel.attr("authority-id", authority.id);
                if (authority.accessScope === 0) {
                    authorityPanel.find(".normal-button").addClass("yes");
                }
                if (authority.accessScope === 1) {
                    authorityPanel.find(".everyone-button").addClass("yes");
                }
                if (authority.accessScope === 2) {
                    authorityPanel.find(".anonymous-button").addClass("yes");
                }
                if (authority.accessScope === -1) {
                    authorityPanel.find(".deny-button").addClass("yes");
                }
                authorityPanel.find(".name-div").text(authority.name);
                authorityPanel.find(".uri-div").text(authority.uri);
                authorityPanel.find(".method-div").text(authority.method);
                authorityPanel.find(".access-scope-button").click(function () {
                    if (!$(this).hasClass("yes")) {
                        changeAccessScope($(this));
                    }
                });
                microservicePanel.find(".authority-list").append(authorityPanel);
            }
        }
    });
}

/**
 * 改变访问权限范围
 * @param accessScopeButton
 */
function changeAccessScope(accessScopeButton) {
    let authorityPanel = $(accessScopeButton.parents(".authority-panel")[0]);
    let authorityId = authorityPanel.attr("authority-id");
    let accessScope = 0;
    if (accessScopeButton.hasClass("everyone-button")) {
        accessScope = 1;
    } else if (accessScopeButton.hasClass("anonymous-button")) {
        accessScope = 2;
    } else if (accessScopeButton.hasClass("deny-button")) {
        accessScope = -1;
    }
    $.ajax({
        type: "PUT",
        url: "/service/authorize/authority/changeAccessScope",
        data: {
            id: authorityId,
            accessScope: accessScope
        },
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        success: function () {
            let accessScopeButtons = authorityPanel.find(".access-scope-button");
            for (let i = 0; i < accessScopeButtons.length; i++) {
                let tempAccessScopeButton = $(accessScopeButtons[i]);
                if (tempAccessScopeButton !== accessScopeButton) {
                    tempAccessScopeButton.removeClass("yes");
                }
            }
            accessScopeButton.addClass("yes");
        }
    });
}

/**
 * 刷新服务列表
 * @param microservicePanel
 */
function refreshService(microservicePanel) {
    let microserviceId = microservicePanel.attr("microservice-id");
    $.ajax({
        type: "PUT",
        url: "/service/authorize/authority/refreshAuthorities/" + microserviceId,
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        success: function () {
            queryUris(microservicePanel);
        }
    });
}

/**
 * 添加按钮点击事件处理
 * 添加新微服务div显示
 */
function addNewHandle() {
    if ($(".microservice-component .add-new-panel").hasClass("disable")) {
        return;
    }

    let addMicroservicePanel = $(".microservice-component .templates .edit-microservice-panel").clone();
    addMicroservicePanel.css("margin-bottom", "0");
    addMicroservicePanel.find(".panel-title").text("Add Microservice");
    addMicroservicePanel.find(".save-microservice-button").click(function () {
        addMicroservice(addMicroservicePanel);
    });
    addMicroservicePanel.find(".cancel-microservice-button").click(function () {
        addMicroservicePanel.remove();
        $(".microservice-component .add-new-panel").removeClass("disable");
    });
    $(".add-microservice-div").append(addMicroservicePanel);
    $(".microservice-component .add-new-panel").addClass("disable");
}

/**
 * 添加微服务
 * @param addMicroservicePanel
 */
function addMicroservice(addMicroservicePanel) {

    let requestData = {};
    requestData.name = addMicroservicePanel.find(".name-value").val();
    requestData.contextPath = addMicroservicePanel.find(".context-path-value").val();
    requestData.description = addMicroservicePanel.find(".description-value").val();

    $.ajax({
        type: 'POST',
        url: "/service/authorize/microservice/add",
        contentType: "application/json;charset=UTF-8",
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        data: JSON.stringify(requestData),
        success: function () {
            addMicroservicePanel.remove();
            $(".microservice-component .add-new-panel").removeClass("disable");
            $(".microservice-component .microservice-list").empty();
            queryMicroserviceList();
        }
    });
}

/**
 * 修改为服务信息
 * @param editMicroservicePanel
 */
function updateMicroservice(editMicroservicePanel) {
    let requestData = {};
    requestData.id = editMicroservicePanel.attr("microservice-id");
    requestData.name = editMicroservicePanel.find(".name-value").val();
    requestData.contextPath = editMicroservicePanel.find(".context-path-value").val();
    requestData.description = editMicroservicePanel.find(".description-value").val();

    $.ajax({
        type: 'PUT',
        url: "/service/authorize/microservice/update",
        contentType: "application/json;charset=UTF-8",
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        data: JSON.stringify(requestData),
        success: function () {
            $(".microservice-component .microservice-list").empty();
            queryMicroserviceList();
        }
    });
}

/**
 * 页面启动完成事件
 *
 * 1.查询微服务列表
 *
 *
 */
$(document).ready(function () {
    queryMicroserviceList();
});
